﻿using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Plumbing.Model;
using YArchitech.Plumbing.PromptInformation;

namespace YArchitech.Plumbing
{
	public partial class FormLayFireVoltageEquipment : HYBaseForm
	{
		public FormLayFireVoltageEquipment()
		{
			this.InitializeComponent();
		}

		private void FormLayFireVoltageEquipment_Load(object sender, EventArgs e)
		{
			this.GetUserSetting();
		}

		private void btnOver_Click(object sender, EventArgs e)
		{
			new FormPictureInfo(Path.Combine(Product.ResLocation, "消防稳压上置式.png"))
			{
				Location = new System.Drawing.Point(base.Location.X + 502, base.Location.Y),
				Text = "上置式"
			}.ShowDialog();
		}

		private void btnUnder_Click(object sender, EventArgs e)
		{
			new FormPictureInfo(Path.Combine(Product.ResLocation, "消防稳压下置式.png"))
			{
				Location = new System.Drawing.Point(base.Location.X + 502, base.Location.Y),
				Text = "下置式"
			}.ShowDialog();
		}

		private void btnAdjustVolumeSafetyFactor_Click(object sender, EventArgs e)
		{
			new FormPromptInformation(PromptMessageUntil.GetSafetyFactor().Message)
			{
				Location = new System.Drawing.Point(base.Location.X + 502, base.Location.Y)
			}.ShowDialog();
		}

		private void btnAdjustVolumeStartTimes_Click(object sender, EventArgs e)
		{
			new FormPromptInformation(PromptMessageUntil.GetStartTimes().Message)
			{
				Location = new System.Drawing.Point(base.Location.X + 502, base.Location.Y)
			}.ShowDialog();
		}

		private void btnLayWXB_Click(object sender, EventArgs e)
		{
			if (this.dataGVDeviceList.RowCount == 0)
			{
				YJKMessageBox.Show("设备列表为空，无法布置！");
				return;
			}
			int index = this.dataGVDeviceList.CurrentRow.Index;
			this.fireVoltageEquipment.Model = this.dataGVDeviceList.Rows[index].Cells[0].GetText();
			this.fireVoltageEquipment.Lift = this.dataGVDeviceList.Rows[index].Cells[1].GetText().ToDouble(0.0);
			this.fireVoltageEquipment.Flow = this.dataGVDeviceList.Rows[index].Cells[2].GetText().ToDouble(0.0);
			this.fireVoltageEquipment.Volume = this.dataGVDeviceList.Rows[index].Cells[3].GetText().ToInt(0);
			this.fireVoltageEquipment.Power = this.dataGVDeviceList.Rows[index].Cells[4].GetText().ToDouble(0.0);
			this.fireVoltageEquipment.Installation = this.dataGVDeviceList.Rows[index].Cells[5].GetText();
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private void btnCal_Click(object sender, EventArgs e)
		{
			if (this.tabctlLocation.SelectedIndex == 0)
			{
				if (this.hytbOver.Text == "")
				{
					YJKMessageBox.Show("H(m)不能为空！");
					this.hytbOver.Focus();
					this.hytbOver.SelectAll();
					return;
				}
				if (this.hytbOver1.Text == "")
				{
					YJKMessageBox.Show("H1(m)不能为空！");
					this.hytbOver1.Focus();
					this.hytbOver1.SelectAll();
					return;
				}
				if (this.hytbOver2.Text == "")
				{
					YJKMessageBox.Show("H2(m)不能为空！");
					this.hytbOver2.Focus();
					this.hytbOver2.SelectAll();
					return;
				}
			}
			else
			{
				if (this.hytbUnderH.Text == "")
				{
					YJKMessageBox.Show("H(m)不能为空！");
					this.hytbUnderH.Focus();
					this.hytbUnderH.SelectAll();
					return;
				}
				if (this.hytbUnderH2.Text == "")
				{
					YJKMessageBox.Show("H2(m)不能为空！");
					this.hytbUnderH2.Focus();
					this.hytbUnderH2.SelectAll();
					return;
				}
			}
			if (this.hytbVoltageFlowQ.Text == "")
			{
				YJKMessageBox.Show("稳压泵流量Q不能为空！");
				this.hytbVoltageFlowQ.Focus();
				this.hytbVoltageFlowQ.SelectAll();
				return;
			}
			if (this.hytbAdjustVolumeSafetyFactor.Text == "")
			{
				YJKMessageBox.Show("安全系数C不能为空！");
				this.hytbAdjustVolumeSafetyFactor.Focus();
				this.hytbAdjustVolumeSafetyFactor.SelectAll();
				return;
			}
			if (this.hytbAdjustVolumeStartTimes.Text == "")
			{
				YJKMessageBox.Show("1h最多启动次数n不能为空！");
				this.hytbAdjustVolumeStartTimes.Focus();
				this.hytbAdjustVolumeStartTimes.SelectAll();
				return;
			}
			this.CalParamValue();
			this.GetDeviceList();
		}

		private void CalParamValue()
		{
			if (this.tabctlLocation.SelectedIndex == 0)
			{
				if (Regex.IsMatch(this.hytbOver1.Text, "^[0-9]+\\d*[.]?\\d*$") && Regex.IsMatch(this.hytbOver2.Text, "^[0-9]+\\d*[.]?\\d*$"))
				{
					double num = Math.Round(0.01 * (15.0 - this.hytbOver1.Text.ToDouble(0.0)), 3, MidpointRounding.AwayFromZero);
					double num2 = Math.Round(0.01 * (this.hytbOver2.Text.ToDouble(0.0) + 7.0), 3, MidpointRounding.AwayFromZero);
					this.hytbVoltageStartPumpPressureP1.Text = ((num > num2) ? num.ToString() : num2.ToString());
					this.hytbVoltageEndPumpPressureP2.Text = Math.Round(this.hytbVoltageStartPumpPressureP1.Text.ToDouble(0.0) / 0.8, 3, MidpointRounding.AwayFromZero).ToString();
					if (Regex.IsMatch(this.hytbOver.Text, "^[0-9]+\\d*[.]?\\d*$"))
					{
						double value = 0.01 * (this.hytbOver1.Text.ToDouble(0.0) + this.hytbOver.Text.ToDouble(0.0) - 7.0) + this.hytbVoltageStartPumpPressureP1.Text.ToDouble(0.0);
						this.hytbFireStartPumpPressureP.Text = Math.Round(value, 3, MidpointRounding.AwayFromZero).ToString();
					}
				}
			}
			else if (Regex.IsMatch(this.hytbUnderH.Text, "^[0-9]+\\d*[.]?\\d*$") && Regex.IsMatch(this.hytbUnderH2.Text, "^[0-9]+\\d*[.]?\\d*$"))
			{
				double num3 = Math.Round(0.01 * (15.0 + this.hytbUnderH.Text.ToDouble(0.0)), 3, MidpointRounding.AwayFromZero);
				double num4 = Math.Round(0.01 * (this.hytbUnderH2.Text.ToDouble(0.0) + 10.0), 3, MidpointRounding.AwayFromZero);
				this.hytbUnderVoltageStartPumpPressureP1.Text = ((num3 > num4) ? num3.ToString() : num4.ToString());
				this.hytbUnderVoltageEndPumpPressureP2.Text = Math.Round(this.hytbUnderVoltageStartPumpPressureP1.Text.ToDouble(0.0) / 0.85, 3, MidpointRounding.AwayFromZero).ToString();
				this.hytbUnderFireStartPumpPressureP.Text = Math.Round(this.hytbUnderVoltageStartPumpPressureP1.Text.ToDouble(0.0) - 0.07, 3, MidpointRounding.AwayFromZero).ToString();
			}
			if (Regex.IsMatch(this.hytbVoltageFlowQ.Text, "^[0-9]+\\d*[.]?\\d*$") && Regex.IsMatch(this.hytbAdjustVolumeSafetyFactor.Text, "^[0-9]+\\d*[.]?\\d*$") && Regex.IsMatch(this.hytbAdjustVolumeStartTimes.Text, "^[0-9]+\\d*[.]?\\d*$") && this.hytbAdjustVolumeStartTimes.Text.ToInt(0) != 0)
			{
				double num5 = this.hytbAdjustVolumeSafetyFactor.Text.ToDouble(0.0) * this.hytbVoltageFlowQ.Text.ToDouble(0.0) * 3600.0 / (double)(4 * this.hytbAdjustVolumeStartTimes.Text.ToInt(0));
				num5 = Math.Round(num5, 2, MidpointRounding.AwayFromZero);
				this.hytbAdjustVolumeVx2.Text = ((num5 > 150.0) ? Math.Ceiling(num5).ToString() : "150");
			}
		}

		private void GetDeviceList()
		{
			try
			{
				double num;
				if (this.tabctlLocation.SelectedIndex == 0)
				{
					num = this.hytbVoltageEndPumpPressureP2.Text.ToDouble(0.0);
				}
				else
				{
					num = this.hytbUnderVoltageEndPumpPressureP2.Text.ToDouble(0.0);
				}
				double num2 = this.hytbVoltageFlowQ.Text.ToDouble(0.0) * 3.6;
				int num3 = this.hytbAdjustVolumeVx2.Text.ToInt(0);
				SQLiteDataOperate.SetDataSource(Path.Combine(Product.DataLocation, "HYBIMSPACE.db3"));
				DataTable dataSource = SQLiteDataOperate.ExecQuery(string.Format("select Model as '型号', Lift as '扬程(Mpa)', Flow as '流量(m³/h)', Volume as '调节容积(L)', Power as '功率(kw)', Installation as '安装形式' from FireVoltageEquipmentInfo where Lift >= {0} and Flow >= {1} and Volume >= {2} order by Lift, Flow, Volume", num, num2, num3));
				this.dataGVDeviceList.DataSource = dataSource;
			}
			catch (Exception)
			{
			}
		}

		private void FormLayFireVoltageEquipment_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		private void GetUserSetting()
		{
			string[] layFireVoltageEquipmentText = this.userSetting.GetLayFireVoltageEquipmentText();
			if (layFireVoltageEquipmentText[0] == "")
			{
				this.hytbOver.Text = "80";
			}
			else
			{
				this.hytbOver.Text = layFireVoltageEquipmentText[0];
			}
			if (layFireVoltageEquipmentText[1] == "")
			{
				this.hytbOver1.Text = "8";
			}
			else
			{
				this.hytbOver1.Text = layFireVoltageEquipmentText[1];
			}
			if (layFireVoltageEquipmentText[2] == "")
			{
				this.hytbOver2.Text = "2";
			}
			else
			{
				this.hytbOver2.Text = layFireVoltageEquipmentText[2];
			}
			if (layFireVoltageEquipmentText[3] == "")
			{
				this.hytbUnderH.Text = "15";
			}
			else
			{
				this.hytbUnderH.Text = layFireVoltageEquipmentText[3];
			}
			if (layFireVoltageEquipmentText[4] == "")
			{
				this.hytbUnderH2.Text = "20";
			}
			else
			{
				this.hytbUnderH2.Text = layFireVoltageEquipmentText[4];
			}
			if (layFireVoltageEquipmentText[5] == "")
			{
				this.hytbVoltageFlowQ.Text = "1";
			}
			else
			{
				this.hytbVoltageFlowQ.Text = layFireVoltageEquipmentText[5];
			}
			if (layFireVoltageEquipmentText[6] == "")
			{
				this.hytbAdjustVolumeSafetyFactor.Text = "1.1";
			}
			else
			{
				this.hytbAdjustVolumeSafetyFactor.Text = layFireVoltageEquipmentText[6];
			}
			if (layFireVoltageEquipmentText[7] == "")
			{
				this.hytbAdjustVolumeStartTimes.Text = "15";
			}
			else
			{
				this.hytbAdjustVolumeStartTimes.Text = layFireVoltageEquipmentText[7];
			}
			if (layFireVoltageEquipmentText[8] != "" && layFireVoltageEquipmentText[9] != "")
			{
				base.Location = new System.Drawing.Point(layFireVoltageEquipmentText[8].ToInt(0), layFireVoltageEquipmentText[9].ToInt(0));
			}
			if (layFireVoltageEquipmentText[10] == "" || layFireVoltageEquipmentText[10] == "0")
			{
				this.tabctlLocation.SelectedIndex = 0;
				return;
			}
			this.tabctlLocation.SelectedIndex = 1;
		}

		private void WriteUserInfo()
		{
			string[] array = new string[11];
			array[0] = this.hytbOver.Text;
			array[1] = this.hytbOver1.Text;
			array[2] = this.hytbOver2.Text;
			array[3] = this.hytbUnderH.Text;
			array[4] = this.hytbUnderH2.Text;
			array[5] = this.hytbVoltageFlowQ.Text;
			array[6] = this.hytbAdjustVolumeSafetyFactor.Text;
			array[7] = this.hytbAdjustVolumeStartTimes.Text;
			array[8] = base.Location.X.ToString();
			array[9] = base.Location.Y.ToString();
			if (this.tabctlLocation.SelectedIndex == 0)
			{
				array[10] = "0";
			}
			else
			{
				array[10] = "1";
			}
			this.userSetting.WriteLayFireVoltageEquipmentText(array);
		}

		private void FormLayFireVoltageEquipment_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.WriteUserInfo();
		}

		private void tabctlLocation_SelectedIndexChanged(object sender, EventArgs e)
		{
			DataTable dataTable = (DataTable)this.dataGVDeviceList.DataSource;
			if (dataTable != null)
			{
				dataTable.Rows.Clear();
				this.dataGVDeviceList.DataSource = dataTable;
			}
		}

		public FireVoltageEquipment fireVoltageEquipment = new FireVoltageEquipment();

		private HVACUserSetting userSetting = new HVACUserSetting();
	}
}
